﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using LinqToTree;
using System.Diagnostics;

namespace LinqToTreeWPF
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();


        }

        private void Window_ContentRendered(object sender, EventArgs e)
        {

            // get all the TextBox's which have a Grid as direct parent
            var itemsFluent = new VisualTreeAdapter(this).Descendants()
                                                   .Where(i => i.Ancestors().First().Item is Grid)
                                                   .Where(i => i.Item is TextBox)
                                                   .Select(i => i.Item);

            var itemsQuery = from v in new VisualTreeAdapter(this).Descendants()
                             where v.Ancestors().First().Item is Grid && v.Item is TextBox
                             select v.Item;

            // get all the StackPanels that are within another StackPanel visual tree
            var items2Fluent = new VisualTreeAdapter(this).Descendants()
                                                   .Where(i => i.Item is StackPanel)
                                                   .Descendants()
                                                   .Where(i => i.Item is StackPanel)
                                                   .Select(i => i.Item);

            var items2Query = from i in
                                  (from v in new VisualTreeAdapter(this).Descendants()
                                   where v.Item is StackPanel
                                   select v).Descendants()
                              where i.Item is StackPanel
                              select i.Item;

            // find all descendant text boxes
            var textBoxes = new VisualTreeAdapter(this).Descendants<DependencyObject, TextBox>();

        }

    }
}
